#include <QtTest>
#include "TreeModel.h"
#include "TreeNode.h"

class TestTreeModel : public QObject
{
    Q_OBJECT

private slots:
    void initTestCase(); // 初始化测试
    void cleanupTestCase(); // 清理测试
    
    // TreeModel 测试
    void testModelInitialization();
    void testDataInsertion();
    void testDataRetrieval();
    void testDataRemoval();
    void testHeaderData();
    void testFlags();
    void testSetData();
    
    // TreeNode 测试
    void testTreeNodeCreation();
    void testTreeNodeData();
    void testTreeNodeParentChild();
    void testTreeNodeInsertRemove();

private:
    TreeModel* model;
    TreeNode* testNode;
};

void TestTreeModel::initTestCase()
{
    model = new TreeModel(this);
}

void TestTreeModel::cleanupTestCase()
{
    delete model;
}

void TestTreeModel::testModelInitialization()
{
    QVERIFY(model != nullptr);
    QCOMPARE(model->rowCount(), 0);
}

void TestTreeModel::testDataInsertion()
{
    // 测试插入数据
    QModelIndex rootIndex;
    model->insertRow(0, rootIndex);
    QCOMPARE(model->rowCount(), 1);
    
    // 插入子节点
    QModelIndex parent = model->index(0, 0);
    model->insertRow(0, parent);
    QCOMPARE(model->rowCount(parent), 1);
}

void TestTreeModel::testDataRetrieval()
{
    // 测试数据检索
    QModelIndex index = model->index(0, 0);
    QVERIFY(index.isValid());
    
    QVariant data = model->data(index, Qt::DisplayRole);
    QVERIFY(data.isValid());
}

void TestTreeModel::testDataRemoval()
{
    // 测试数据删除
    QModelIndex index = model->index(0, 0);
    model->removeRow(0, index.parent());
    QCOMPARE(model->rowCount(), 0);
}

void TestTreeModel::testHeaderData()
{
    QVariant header = model->headerData(0, Qt::Horizontal, Qt::DisplayRole);
    QVERIFY(header.isValid());
    QCOMPARE(header.toString(), QString("Column 0"));
}

void TestTreeModel::testFlags()
{
    QModelIndex index = model->index(0, 0);
    Qt::ItemFlags flags = model->flags(index);
    QVERIFY(flags & Qt::ItemIsEnabled);
    QVERIFY(flags & Qt::ItemIsSelectable);
}

void TestTreeModel::testSetData()
{
    QModelIndex index = model->index(0, 0);
    bool result = model->setData(index, QVariant("Test Data"), Qt::EditRole);
    QVERIFY(result);
    QCOMPARE(model->data(index, Qt::DisplayRole).toString(), QString("Test Data"));
}

void TestTreeModel::testTreeNodeCreation()
{
    testNode = new TreeNode("Root");
    QVERIFY(testNode != nullptr);
    QCOMPARE(testNode->data(0).toString(), QString("Root"));
}

void TestTreeModel::testTreeNodeData()
{
    testNode = new TreeNode("Node1");
    testNode->setData(0, "New Data");
    QCOMPARE(testNode->data(0).toString(), QString("New Data"));
}

void TestTreeModel::testTreeNodeParentChild()
{
    TreeNode* parent = new TreeNode("Parent");
    TreeNode* child = new TreeNode("Child");
    
    parent->appendChild(child);
    QCOMPARE(child->parent(), parent);
    QCOMPARE(parent->childCount(), 1);
    QCOMPARE(parent->child(0), child);
}

void TestTreeModel::testTreeNodeInsertRemove()
{
    TreeNode* parent = new TreeNode("Parent");
    TreeNode* child1 = new TreeNode("Child1");
    TreeNode* child2 = new TreeNode("Child2");
    
    parent->insertChild(0, child1);
    parent->insertChild(1, child2);
    QCOMPARE(parent->childCount(), 2);
    
    parent->removeChild(0);
    QCOMPARE(parent->childCount(), 1);
    QCOMPARE(parent->child(0), child2);
}

QTEST_MAIN(TestTreeModel)
#include "tst_treemodel.moc"
